package rjeutsky.models;

import java.util.LinkedList;
import java.util.List;

import rjeutsky.models.builders.ConcreteOldCardBuilder;
import rjeutsky.models.builders.OldCardBuilder;
import rjeutsky.models.builders.RandomOldCardBuilder;
import rjeutsky.xml.OldCardFileSerializer;
import rjeutsky.xml.dom.OldCardDomSerializer;
import rjeutsky.xml.sax.OldCardSaxSerializer;
import rjeutsky.xml.stax.OldCardStaxSerializer;

public class ApplicationModel {
	private OldCardFileSerializer serializer;
	private List<OldCard> cards = new LinkedList<OldCard>();
	
	public OldCardFileSerializer getSerializer() {
		return serializer;
	}
	
	protected void setSerializer(OldCardFileSerializer serializer) {
		this.serializer = serializer;
	}
	
	public List<OldCard> getCards() {
		return cards;
	}
	
	protected void setCards(List<OldCard> cards) {
		this.cards = cards;
	}
	
	public void clearCards(){
		cards.clear();
	}
	
	public void initRandomData(int cardsCount){
		cards.clear();
		OldCardBuilder builder = getRandomBuilder();
		for (int i = 0; i< cardsCount; i++){
			builder.createNewCard();
			builder.buildAll();
			cards.add(builder.getCard());
		}
	}
	
	public void addNewCard(CardThema cardThema, boolean isSent, TypeEnum type, String country, int year, String author, String valuable){
		ConcreteOldCardBuilder builder = new ConcreteOldCardBuilder();
		builder.setThema(cardThema);
		CardType cardType = new CardType();
		cardType.setIsSent(isSent);
		cardType.setType(type);
		builder.setType(cardType);
		builder.setCountry(country);
		builder.setYear(year);
		builder.setAuthor(author);
		builder.setValuable(valuable);
		
		builder.createNewCard();
		builder.buildAll();
		cards.add(builder.getCard());
	}
	
	private OldCardBuilder getRandomBuilder(){
		RandomOldCardBuilder builder = new RandomOldCardBuilder();
		List<String> authors = new LinkedList<String>();
		authors.add("John Jackson");
		authors.add("Jack Johnson");
		builder.setAuthors(authors);
		List<String> countries = new LinkedList<String>();
		countries.add("Australia");
		countries.add("USA");
		builder.setCountries(countries);
		List<String> valualities = new LinkedList<String>();
		valualities.add("historical value");
		valualities.add("collector value");
		builder.setValuablities(valualities);
		return builder;
	}
	
	public boolean Save(String filePath) {
		serializer.setFilePath(filePath);
		serializer.setOldCardList(cards);
		return serializer.serializeToFile();
	}
	
	public boolean Load(String filePath) {
		serializer.setFilePath(filePath);
		if (serializer.deserializeFromFile()) {
			cards = serializer.getOldCardList();
			return true;
		}
		return false;
	}
	
	public void setSAX(){
		serializer = new OldCardSaxSerializer();
	}
	
	public void setSTAX(){
		serializer = new OldCardStaxSerializer();
	}
	
	public void setDOM() {
		serializer = new OldCardDomSerializer();
	}

}
